/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DSPhong.java
 *
 * Created on Dec 20, 2012, 4:12:46 AM
 */
package CLIENT;

import BLL.RoomBLL;
import DTO.Room;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import java.sql.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Huong
 */
public class DSPhong extends javax.swing.JFrame {
    
    /** Creates new form DSPhong */
    public DSPhong() throws Exception {
        initComponents();  
        loadData();
        }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        bttPhongtrong = new javax.swing.JButton();
        txtTang = new javax.swing.JTextField();
        bttSaptrong = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mã Phòng", "Tầng", "Phòng", "Giá", "Tình trạng"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Về Menu");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        bttPhongtrong.setText("Tìm phòng trống");
        bttPhongtrong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bttPhongtrongActionPerformed(evt);
            }
        });

        bttSaptrong.setText("Phòng sắp hết hạn");
        bttSaptrong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bttSaptrongActionPerformed(evt);
            }
        });

        jLabel1.setText("Tầng:");

        jButton2.setText("Sửa giá tiền");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(51, 51, 51)
                        .addComponent(jLabel1)
                        .addGap(31, 31, 31)
                        .addComponent(txtTang, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(82, 82, 82)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(bttSaptrong, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(bttPhongtrong, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE))
                        .addGap(57, 57, 57)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 567, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(257, 257, 257)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(26, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(bttPhongtrong)
                        .addGap(13, 13, 13)
                        .addComponent(bttSaptrong))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtTang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))))
                .addGap(24, 24, 24)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addGap(24, 24, 24))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
 private void loadData() throws Exception
    {
      ArrayList<Room> list=RoomBLL.selectRoomall();
    DefaultTableModel dtm=new DefaultTableModel();
    dtm.addColumn("Mã phòng");
    dtm.addColumn("Tầng");
    dtm.addColumn("Phòng");
    dtm.addColumn("Giá");
    dtm.addColumn("Tình trạng");
    for (int i=0; i<list.size(); i++){
        Room emp=list.get(i);
        Vector vec=new Vector();
        vec.add(emp.getMaPhong());
        vec.add(emp.getTang());
        vec.add(emp.getPhong());
        vec.add(emp.getGia());
        if(emp.isTinhTrang()){
        vec.add("Đã thuê");
        }
        else
        {
            vec.add("Trống");
        }
        dtm.addRow(vec);
    }
    this.jTable1.setModel(dtm);
}

    
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    Main m =new Main();
    m.show();
    this.dispose();
}//GEN-LAST:event_jButton1ActionPerformed

private void bttPhongtrongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bttPhongtrongActionPerformed
        try {
            int key = 0;
            key=Integer.parseInt(this.txtTang.getText());
            ArrayList<Room> list = null;
                 list = RoomBLL.selectTrong(key);
         DefaultTableModel dtm=new DefaultTableModel();
         dtm.addColumn("Mã phòng");
         dtm.addColumn("Tầng");
         dtm.addColumn("Phòng");
         dtm.addColumn("Giá");
         dtm.addColumn("Tình trạng");
         for (int i=0; i<list.size(); i++){
             Room emp=list.get(i);
             Vector vec=new Vector();
             vec.add(emp.getMaPhong());
             vec.add(emp.getTang());
             vec.add(emp.getPhong());
             vec.add(emp.getGia());
             vec.add("Trống");
              dtm.addRow(vec);
         }
         this.jTable1.setModel(dtm);
        } catch (SQLException ex) {
            Logger.getLogger(DSPhong.class.getName()).log(Level.SEVERE, null, ex);
        }
           
       
}//GEN-LAST:event_bttPhongtrongActionPerformed

private void bttSaptrongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bttSaptrongActionPerformed
      try {
            int key = 0;
            key=Integer.parseInt(this.txtTang.getText());
            Date day = new Date(System.currentTimeMillis());
            ArrayList<Room> list = null;
            list = RoomBLL.selectTra(key, day);
         DefaultTableModel dtm=new DefaultTableModel();
         dtm.addColumn("Mã phòng");
         dtm.addColumn("Tầng");
         dtm.addColumn("Phòng");
         dtm.addColumn("Giá");
         dtm.addColumn("Ngày Trả");
         for (int i=0; i<list.size(); i++){
             Room emp=list.get(i);
             Vector vec=new Vector();
             vec.add(emp.getMaPhong());
             vec.add(emp.getTang());
             vec.add(emp.getPhong());
             vec.add(emp.getGia());
             vec.add(emp.getNgayTra());
              dtm.addRow(vec);
         }
         this.jTable1.setModel(dtm);
        } catch (SQLException ex) {
            Logger.getLogger(DSPhong.class.getName()).log(Level.SEVERE, null, ex);
        }
             
}//GEN-LAST:event_bttSaptrongActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
    int row = this.jTable1.getSelectedRow();
    if (row >= 0) {
    String ma = this.jTable1.getValueAt(row, 0).toString();
    String giaCu = this.jTable1.getValueAt(row, 3).toString();
    SuaGia sg=new SuaGia(this);
    sg.show();
    this.dispose();
    sg.setVisible(true);
    sg.setma(ma);
    sg.setGiaCu(giaCu);
    }
    else {
        JOptionPane.showMessageDialog(this, "Vui lòng chọn phòng cần thay đổi giá");
    }
    
}//GEN-LAST:event_jButton2ActionPerformed

    /**
 *
     * @param args the command line arguments
     */ public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DSPhong.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DSPhong.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DSPhong.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DSPhong.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    new DSPhong().setVisible(true);
                } catch (Exception ex) {
                    Logger.getLogger(DSPhong.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
     
  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bttPhongtrong;
    private javax.swing.JButton bttSaptrong;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField txtTang;
    // End of variables declaration//GEN-END:variables
}
